#include "buttonrelay.h"
#include "relay.h"
#include <QApplication>
#include <QPainter>

ButtonRelay::ButtonRelay()
{
    mPixmapBackground.load(tr("://images/btn_background.png"));

    mPixmapOn.load(tr("://images/status_on.png"));
    mPixmapOff.load(tr("://images/status_off.png"));
    mPixmapReady.load(tr("://images/status_ready.png"));
    mPixmapPending.load(tr("://images/status_pending.png"));
    mPixmapUnknown.load(tr("://images/status_unknown.png"));

    mName = "Relay";
    mBId = 0;
    mRId = 0;
    mIsCurrent = false;
    mCurrent = 0;
    setStatus(RELAY_STATUS_OFF);
}

ButtonRelay::ButtonRelay(QString& name, int bid, int rid, int status, bool isCurrent)
{
    mPixmapBackground.load(tr("://images/btn_background.png"));

    mPixmapOn.load(tr("://images/status_on.png"));
    mPixmapOff.load(tr("://images/status_off.png"));
    mPixmapReady.load(tr("://images/status_ready.png"));
    mPixmapPending.load(tr("://images/status_pending.png"));
    mPixmapUnknown.load(tr("://images/status_unknown.png"));

    mName = name;
    mBId = bid;
    mRId = rid;
    mIsCurrent = isCurrent;
    mCurrent = 0;
    setStatus(status);
}

QSize ButtonRelay::sizeHint() const
{
    QSize size = QPushButton::sizeHint();

    if (mPixmapBackground.isNull()) {
        size.setWidth(DefWidth);
        size.setHeight(DefHeight);
    } else {
        size = mPixmapBackground.size();
    }

    return size;
}

void ButtonRelay::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QPainter painter;

    painter.begin(this);
    if (false == mPixmapBackground.isNull()) {
        painter.drawPixmap(0, 0, mPixmapBackground);
        painter.drawText(QRectF(0, 0, mPixmapBackground.size().width(), 28), Qt::AlignCenter, mName);
        if (false == mPixmapStatus.isNull()) {
            painter.drawPixmap(4, 28, mPixmapStatus);
        }
        if (mIsCurrent && (RELAY_STATUS_ON<=mStatus && RELAY_STATUS_HEATING>=mStatus)) {
            painter.drawText(QRectF(0, 0, mPixmapBackground.size().width(), 200), Qt::AlignCenter, QString("%1").arg(mCurrent));
        }
    } else {
        painter.drawText(QRectF(0, 0, DefWidth, DefHeight), Qt::AlignCenter, mName);
    }
    painter.end();
}

void ButtonRelay::setStatus(int status)
{
    QString fileName;

    switch (status) {
    case RELAY_STATUS_OFF:
        mPixmapStatus = mPixmapOff;
        if (false == isEnabled()) {
            setEnabled(true);
        }
        break;
    case RELAY_STATUS_ON:
        if(0 != mIsCurrent)
            mPixmapStatus = mPixmapReady;
        else
            mPixmapStatus = mPixmapOn;

        if (false == isEnabled()) {
            setEnabled(true);
        }
        break;
    case RELAY_STATUS_AIR:
        fileName = tr("://images/status_air.png");
        break;
    case RELAY_STATUS_COOLING:
        fileName = tr("://images/status_cooling.png");
        break;
    case RELAY_STATUS_HEATING:
        fileName = tr("://images/status_heating.png");
        break;

    case RELAY_STATUS_PENDING:
        mPixmapStatus = mPixmapPending;
        if (false == isEnabled()) {
            setEnabled(true);
        }
        break;
    case RELAY_STATUS_UNKNOWN:
        mPixmapStatus = mPixmapUnknown;
        if (false == isEnabled()) {
            setEnabled(true);
        }
        break;
    case RELAY_STATUS_NOTUSED:
    default:
        mPixmapStatus = mPixmapOff;
        setEnabled(false);
        break;
    }

    if (fileName.length()) {
        mPixmapStatus.load(fileName);

        if (false == isEnabled()) {
            setEnabled(true);
        }
    }

    mStatus = status;

    update();
}

int ButtonRelay::status()
{
    return mStatus;
}

void ButtonRelay::setCurrent(int current)
{
    if (mCurrent != current) {
        mCurrent = current;
        if (mIsCurrent && (RELAY_STATUS_ON<=mStatus && RELAY_STATUS_HEATING>=mStatus)) {
            update();
        }
    }
}
